/**
 * 版本管理管理初始化
 */
var DcConfigVersion = {
    id: "DcConfigVersionTable",    //表格id
    seItem: null,        //选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
DcConfigVersion.initColumn = function () {
    return [
        {field: 'selectItem', radio: true},
//            {title: '主键', field: 'id', visible: true, align: 'center', valign: 'middle'},
//            {title: '应用id', field: 'appId', visible: true, align: 'center', valign: 'middle'},
//            {title: '环境id', field: 'envId', visible: true, align: 'center', valign: 'middle'},
            {title: '版本号', field: 'version', visible: true, align: 'center', valign: 'middle'},
            {title: '配置key', field: 'configKey', visible: true, align: 'center', valign: 'middle'},
            {title: '配置value', field: 'configValue', visible: true, align: 'center', valign: 'middle'},
            {title: '配置说明', field: 'configDesc', visible: true, align: 'center', valign: 'middle'},
            {title: '更改类型', field: 'changeTypeDesc', visible: true, align: 'center', valign: 'middle'},
            {title: '创建时间', field: 'createTime', visible: true, align: 'center', valign: 'middle'},
            {title: '创建用户', field: 'createUser', visible: true, align: 'center', valign: 'middle'}
    ];
};

/**
 * 检查是否选中
 */
DcConfigVersion.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if(selected.length == 0){
        Feng.info("请先选中表格中的某一记录！");
        return false;
    }else{
        DcConfigVersion.seItem = selected[0];
        return true;
    }
};

/**
 * 点击添加版本管理
 */
DcConfigVersion.openAddDcConfigVersion = function () {
    var index = layer.open({
        type: 2,
        title: '添加版本管理',
        area: ['800px', '420px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/dcConfigVersion/dcConfigVersion_add'
    });
    this.layerIndex = index;
};
DcConfigVersion.rollback = function () {
    var index = layer.open({
        type: 2,
        title: '版本回滚',
        area: ['800px', '420px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/dcConfigVersion/dcConfigVersion_rollback?appId=' + $("#appId").val() +"&envId=" + $("#envId").val()
    });
    this.layerIndex = index;
};
/**
 * 打开查看版本管理详情
 */
DcConfigVersion.openDcConfigVersionDetail = function () {
    if (this.check()) {
        var index = layer.open({
            type: 2,
            title: '版本管理详情',
            area: ['800px', '420px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/dcConfigVersion/dcConfigVersion_update/' + DcConfigVersion.seItem.id
        });
        this.layerIndex = index;
    }
};

/**
 * 删除版本管理
 */
DcConfigVersion.delete = function () {
    if (this.check()) {
        var ajax = new $ax(Feng.ctxPath + "/dcConfigVersion/delete", function (data) {
            if (data.code == 200){
                Feng.success("删除成功!");
                DcConfigVersion.table.refresh();
            }else{
                Feng.error("删除失败!" + data.message + "!");
            }
        });
        ajax.set("dcConfigVersionId",this.seItem.id);
         Feng.confirm("确认删除此条数据?",function(){
    	        ajax.start();
    	 })
    }
};
DcConfigVersion.appList = function() {
	var ajax = new $ax(Feng.ctxPath + "/dcConfig/appList", function (data) {
		 $.each(data, function(index, item) {
	            $("#appId").append(  //此处向select中循环绑定数据
	    "<option value="+item.id+">" + item.name+ "</option>");
	        });
	});
	ajax.start();
};
DcConfigVersion.envList = function() {
	var ajax = new $ax(Feng.ctxPath + "/dcConfig/envList", function (data) {
		 $.each(data, function(index, item) {
	            $("#envId").append(  //此处向select中循环绑定数据
	    "<option value="+item.id+">" + item.name+ "</option>");
	        });
	});
	ajax.start();
};
DcConfigVersion.formParams = function() {
    var queryData = {};
    queryData['appId'] = $("#appId").val();
    queryData['envId'] = $("#envId").val();
    queryData['version'] = $("#version").val();
    queryData['configKey'] = $("#configKey").val();
    return queryData;
}
/**
 * 查询版本管理列表
 */
DcConfigVersion.search = function () {
    DcConfigVersion.table.refresh({query: DcConfigVersion.formParams()});
};
/**
 * 下载配置
 */
DcConfigVersion.download = function () {
	window.location.href=Feng.ctxPath + "/dcConfigVersion/download?appId=" + $("#appId").val() +"&envId=" + $("#envId").val() + "&version="+ $("#version").val()+ "&configKey="+ $("#configKey").val()
};
$(function () {
	DcConfigVersion.appList();
	DcConfigVersion.envList();
    var defaultColunms = DcConfigVersion.initColumn();
    var table = new BSTable(DcConfigVersion.id, "/dcConfigVersion/list", defaultColunms);
    table.setPaginationType("server");
    table.setQueryParams(DcConfigVersion.formParams());
    DcConfigVersion.table = table.init();
    $('#appId').change(function(){  
    	DcConfigVersion.table.refresh({query: DcConfigVersion.formParams()});
      })  
      $('#envId').change(function(){  
    	  DcConfigVersion.table.refresh({query: DcConfigVersion.formParams()});
      })  
      if ($('#appId').val()== null ||  $('#envId').val() == null){
      	$(":input").attr({"disabled":"disabled"});
      }
});
